﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using System.IO;
using System.Threading;

namespace AssemblyCYG
{
    public partial class BlobDetect : Form
    {
        bool isPaintShape = false;
        HObject unionROI = null;
        HObject ho_Image = null;
        HObject ho_ImageReduced = null;
        HTuple hv_ImageWidth = null;
        HTuple hv_ImageHeight = null;
        HTuple hv_Channel = null;
        double scaleS_X = 0;
        double scaleS_Y = 0;
        double scaleE_X = 0;
        double scaleE_Y = 0;
        int keyNum = 0;
        int wheelNum = 0;
        int minGray = 0;
        int maxGray = 0;
        Point mouseDownPoint = new Point();
        System.Diagnostics.Stopwatch detectTime = new System.Diagnostics.Stopwatch();   //记录各工具的检测时间
        System.Diagnostics.Stopwatch mouseDownTime = new System.Diagnostics.Stopwatch();
        HalconAlgorithm halconAlgorithm = new HalconAlgorithm();
        HObject ho_defectContour = null;
        HObject ho_AffinTransRegion = null;
        public BlobDetectPara outBlobDetectPara = new BlobDetectPara();
        MatchPara curMatchPara = new MatchPara();
        string modelImageFold = Path.Combine(System.Environment.CurrentDirectory, "model", "upCamLocate");
        public BlobDetect(BlobDetectPara inBlobDetectPara, string mImagePath, MatchPara inMatchPara)
        {
            InitializeComponent();
            InitialControls();
            modelImageFold = mImagePath;
            outBlobDetectPara.absThreshold = inBlobDetectPara.absThreshold;
            outBlobDetectPara.addArea = inBlobDetectPara.addArea;
            outBlobDetectPara.addMean = inBlobDetectPara.addMean;
            outBlobDetectPara.addNum = inBlobDetectPara.addNum;
            outBlobDetectPara.areaFilter = inBlobDetectPara.areaFilter;
            outBlobDetectPara.closeRadius = inBlobDetectPara.closeRadius;
            outBlobDetectPara.detectType = inBlobDetectPara.detectType;
            outBlobDetectPara.lightDark = inBlobDetectPara.lightDark;
            outBlobDetectPara.mask = inBlobDetectPara.mask;
            outBlobDetectPara.maxArea = inBlobDetectPara.maxArea;
            outBlobDetectPara.maxGray = inBlobDetectPara.maxGray;
            outBlobDetectPara.maxMean = inBlobDetectPara.maxMean;
            outBlobDetectPara.maxNum = inBlobDetectPara.maxNum;
            outBlobDetectPara.meanRadius = inBlobDetectPara.meanRadius;
            outBlobDetectPara.minArea = inBlobDetectPara.minArea;
            outBlobDetectPara.minGray = inBlobDetectPara.minGray;
            outBlobDetectPara.minMean = inBlobDetectPara.minMean;
            outBlobDetectPara.minNum = inBlobDetectPara.minNum;
            outBlobDetectPara.openRadius = inBlobDetectPara.openRadius;
            outBlobDetectPara.stdDevScale = inBlobDetectPara.stdDevScale;
            if (inBlobDetectPara.detectROI != null)
                outBlobDetectPara.detectROI = inBlobDetectPara.detectROI;
            LoadFitCirclePara();    //加载参数
            curMatchPara.modelLocationRow = inMatchPara.modelLocationRow;   //加载当前匹配参数
            curMatchPara.modelLocationCol = inMatchPara.modelLocationCol;
            curMatchPara.modelMatchType = inMatchPara.modelMatchType;
            curMatchPara.seekAngleStart = inMatchPara.seekAngleStart;
            curMatchPara.seekAngleRange = inMatchPara.seekAngleRange;
            curMatchPara.seekModelNum = inMatchPara.seekModelNum;
            curMatchPara.seekModelScore = inMatchPara.seekModelScore;
        }
        private void LoadFitCirclePara()
        {
            this.cB_detectType.SelectedIndex = outBlobDetectPara.detectType;  //检测方式
            this.NUD_MinGray.Value = outBlobDetectPara.minGray;  //普通阈值最小灰度
            this.NUD_MaxGray.Value = outBlobDetectPara.maxGray;  //普通阈值最Da灰度
            this.VarMask.Value = outBlobDetectPara.mask;         //var阈值
            if (outBlobDetectPara.meanRadius >= 1)
                this.tB_MeanRadius.Text = outBlobDetectPara.meanRadius.ToString();   //模糊因子
            else
                this.tB_MeanRadius.Text = "1";
            this.tB_VarDeviation.Text = outBlobDetectPara.stdDevScale.ToString("f2");  //vask偏差因子
            this.VarThreshold.Value = outBlobDetectPara.absThreshold;                  //vask阈值
            if (outBlobDetectPara.lightDark > 1) this.cB_BlackOrWhite.SelectedIndex = 0;
            else this.cB_BlackOrWhite.SelectedIndex = outBlobDetectPara.lightDark;     //检测类型
            this.ClosingRadius.Text = outBlobDetectPara.closeRadius.ToString("f2");    //闭运算
            this.OpeningRadius.Text = outBlobDetectPara.openRadius.ToString("f2");     //开运算
            if (outBlobDetectPara.areaFilter > 0)
                this.areaFilter.Value = outBlobDetectPara.areaFilter;
            else
                this.areaFilter.Value = 10;
            this.defectMinArea.Text = (outBlobDetectPara.minArea).ToString(); //最小面积
            this.defectMaxArea.Text = (outBlobDetectPara.maxArea).ToString();  //最大面积
            this.defectMinMean.Value = outBlobDetectPara.minMean; //最小亮度
            this.defectMaxMean.Value = outBlobDetectPara.maxMean; //最大亮度
            this.defectMinNum.Value = outBlobDetectPara.minNum;   //最小数量
            this.defectMaxNum.Value = outBlobDetectPara.maxNum;   //最大数量
            if (outBlobDetectPara.addArea)
            {
                this.cB_isAddArea.Checked = true;
            }
            else
            {
                this.cB_isAddArea.Checked = false;
            }
            if (outBlobDetectPara.addMean)
            {
                this.cB_isAddMean.Checked = true;
            }
            else
            {
                this.cB_isAddMean.Checked = false;
            }
            if (outBlobDetectPara.addNum)
            {
                this.cB_isAddNum.Checked = true;
            }
            else
            {
                this.cB_isAddNum.Checked = false;
            }
            if (outBlobDetectPara.detectROI != null)
            {
                if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                unionROI.Dispose();
                unionROI = outBlobDetectPara.detectROI.Clone();
            }
            else
            {
                HOperatorSet.GenEmptyObj(out unionROI);
                unionROI = null;
            }
            if (outBlobDetectPara.isInheritModel)
                this.cB_inheritModel.Checked = true;
            else
                this.cB_inheritModel.Checked = false;
        }
        private void InitialControls()
        {
            this.cB_RoiRelation.SelectedIndex = 0;
            //this.cB_FitCircle_seekPath.SelectedIndex = 0;
            HOperatorSet.SetDraw(hWindowControl1.HalconWindow, "margin");
            HOperatorSet.SetLineWidth(hWindowControl1.HalconWindow, 2);
        }

        #region 绘图及窗体鼠标操作事件
        private void btn_DrawRect_Click(object sender, EventArgs e)
        {
            hWindowControl1.Focus();
            HObject ho_Rectangle;
            HTuple hv_Row1 = null, hv_Column1 = null;
            HTuple hv_Row2 = null, hv_Column2 = null;
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
            isPaintShape = true;
            this.tableLayoutPanel3.Enabled = false;
            HOperatorSet.DrawRectangle1(hWindowControl1.HalconWindow, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2);
            this.tableLayoutPanel3.Enabled = true;
            isPaintShape = false;
            ho_Rectangle.Dispose();
            HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
            switch (this.cB_RoiRelation.SelectedIndex)
            {
                case 0:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Union2(ho_Rectangle, unionROI, out unionROI);
                    ShowDetectContour();
                    break;
                case 1:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Intersection(unionROI, ho_Rectangle, out unionROI);
                    ShowDetectContour();
                    break;
                case 2:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Difference(unionROI, ho_Rectangle, out unionROI);
                    ShowDetectContour();
                    break;
            }
            ho_Rectangle.Dispose();
        }
        private void btn_drawRoRect_Click(object sender, EventArgs e)
        {
            hWindowControl1.Focus();
            HObject ho_Rectangle;
            HTuple hv_Row = null, hv_Column = null;
            HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null;
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
            isPaintShape = true;
            this.tableLayoutPanel3.Enabled = false;
            HOperatorSet.DrawRectangle2(hWindowControl1.HalconWindow, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2);
            this.tableLayoutPanel3.Enabled = true;
            isPaintShape = false;
            ho_Rectangle.Dispose();
            HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2);
            switch (this.cB_RoiRelation.SelectedIndex)
            {
                case 0:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Union2(ho_Rectangle, unionROI, out unionROI);
                    ShowDetectContour();
                    break;
                case 1:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Intersection(unionROI, ho_Rectangle, out unionROI);
                    ShowDetectContour();
                    break;
                case 2:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Difference(unionROI, ho_Rectangle, out unionROI);
                    ShowDetectContour();
                    break;
            }
            ho_Rectangle.Dispose();
        }
        private void btn_drawCircle_Click(object sender, EventArgs e)
        {
            hWindowControl1.Focus();
            HTuple hv_Row = null, hv_Column = null, hv_Radius = null;
            HObject ho_Circle;
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
            isPaintShape = true;
            this.tableLayoutPanel3.Enabled = false;
            HOperatorSet.DrawCircle(hWindowControl1.HalconWindow, out hv_Row, out hv_Column, out hv_Radius);
            this.tableLayoutPanel3.Enabled = true;
            isPaintShape = false;
            ho_Circle.Dispose();
            HOperatorSet.GenCircle(out ho_Circle, hv_Row, hv_Column, hv_Radius);
            switch (this.cB_RoiRelation.SelectedIndex)
            {
                case 0:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Union2(ho_Circle, unionROI, out unionROI);
                    ShowDetectContour();
                    break;
                case 1:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Intersection(unionROI, ho_Circle, out unionROI);
                    ShowDetectContour();
                    break;
                case 2:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Difference(unionROI, ho_Circle, out unionROI);
                    ShowDetectContour();
                    break;
            }
            ho_Circle.Dispose();
        }
        private void btn_drawPoly_Click(object sender, EventArgs e)
        {
            hWindowControl1.Focus();
            HObject ho_Region;
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
            isPaintShape = true;
            ho_Region.Dispose();
            this.tableLayoutPanel3.Enabled = false;
            HOperatorSet.DrawRegion(out ho_Region, hWindowControl1.HalconWindow);
            this.tableLayoutPanel3.Enabled = true;
            isPaintShape = false;
            switch (this.cB_RoiRelation.SelectedIndex)
            {
                case 0:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Union2(ho_Region, unionROI, out unionROI);
                    ShowDetectContour();
                    break;
                case 1:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Intersection(unionROI, ho_Region, out unionROI);
                    ShowDetectContour();
                    break;
                case 2:
                    if (unionROI == null) HOperatorSet.GenEmptyObj(out unionROI);
                    HOperatorSet.Difference(unionROI, ho_Region, out unionROI);
                    ShowDetectContour();
                    break;
            }
            ho_Region.Dispose();
        }
        private void btn_clearROI_Click(object sender, EventArgs e)
        {
            if (ho_Image != null)
            {
                HOperatorSet.GenEmptyObj(out unionROI);
                HOperatorSet.GenEmptyObj(out ho_defectContour);
                //HOperatorSet.GenEmptyObj(out ho_defectUnionContour);
                //HOperatorSet.GenEmptyObj(out AffineTransUnionRegion);
                unionROI = null;
                hWindowControl1.HalconWindow.ClearWindow();
                HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                hWindowControl1.Refresh();
            }
        }
        private void cB_RoiRelation_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void ShowDetectContour()
        {
            hWindowControl1.HalconWindow.ClearWindow();
            if (ho_Image != null) HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
            if (unionROI != null && unionROI.IsInitialized()) HOperatorSet.DispObj(unionROI, hWindowControl1.HalconWindow);
            //HOperatorSet.SetColor(hWindowControl1.HalconWindow, "red");
            //if (ho_defectUnionContour != null && this.cB_showDefectContour.Checked) HOperatorSet.DispObj(ho_defectUnionContour, hWindowControl1.HalconWindow);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "green");
            if (ho_defectContour != null) HOperatorSet.DispObj(ho_defectContour, hWindowControl1.HalconWindow);
            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "cyan");
            if (ho_AffinTransRegion != null) HOperatorSet.DispObj(ho_AffinTransRegion, hWindowControl1.HalconWindow);
            //ShowProductIndex();    //显示产品序列
            hWindowControl1.Refresh();
        }
        private void 清除检测线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ho_Image != null)
            {
                HOperatorSet.GenEmptyObj(out ho_defectContour);
                HOperatorSet.GenEmptyObj(out ho_AffinTransRegion);
                hWindowControl1.HalconWindow.ClearWindow();
                HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                if (unionROI != null) HOperatorSet.DispObj(unionROI, hWindowControl1.HalconWindow);
                hWindowControl1.Refresh();
            }
        }
        private void ho_AdaptiveImage(int hv_ImageWidth, int hv_ImageHeight)
        {
            double rWindow = hWindowControl1.Width * 1.0f / hWindowControl1.Height;
            double rImage = hv_ImageWidth * 1.0f / hv_ImageHeight;
            if (rWindow > rImage)
            {
                double nw = (hv_ImageWidth * rWindow / rImage - hv_ImageWidth) / 2;
                double showWidth = nw + hv_ImageWidth;
                HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, nw * (-1), hv_ImageHeight, showWidth);
                scaleS_Y = 0;
                scaleS_X = nw * (-1);
                scaleE_Y = hv_ImageHeight;
                scaleE_X = showWidth;
            }
            else
            {
                double nh = (hWindowControl1.Height - hWindowControl1.Width / rImage) * hv_ImageHeight / hWindowControl1.Height;
                double showHeight = nh + hv_ImageHeight;
                HOperatorSet.SetPart(hWindowControl1.HalconWindow, nh * (-1), 0, showHeight, hv_ImageWidth);
                scaleS_Y = nh * (-1);
                scaleS_X = 0;
                scaleE_Y = showHeight;
                scaleE_X = hv_ImageWidth;
            }
        }
        private void 固定框放大ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ho_Image != null)
            {
                HTuple hv_Row1 = null, hv_Column1 = null;
                HTuple hv_Row2 = null, hv_Column2 = null;
                hWindowControl1.Focus();
                isPaintShape = true;
                HOperatorSet.DrawRectangle1(hWindowControl1.HalconWindow, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2);
                isPaintShape = false;
                double ratio = hv_ImageWidth.D / hv_ImageHeight.D;
                double rowLength = hv_Row2.D - hv_Row1.D;
                double colLength = hv_Column2.D - hv_Column1.D;
                if (rowLength <= 5 || colLength <= 5) return;
                if (rowLength < colLength)
                {
                    double newColLength = rowLength * ratio;
                    hv_Column2 = hv_Column1 + newColLength;
                }
                else
                {
                    double newRowLength = colLength / ratio;
                    hv_Row2 = hv_Row1 + newRowLength;
                }
                HOperatorSet.SetPart(hWindowControl1.HalconWindow, hv_Row1, hv_Column1, hv_Row2, hv_Column2);
                scaleS_Y = hv_Row1;
                scaleS_X = hv_Column1;
                scaleE_Y = hv_Row2;
                scaleE_X = hv_Column2;
                ShowDetectContour();
            }
        }
        private void 自适应图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ho_Image != null)
            {
                ho_AdaptiveImage(hv_ImageWidth, hv_ImageHeight);
                ShowDetectContour();
            }
        }
        private void 加载图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图片文件(*.jpg,*.tif,*.bmp,*.Png)|*.jpg;*.tif;*.bmp;*.Png";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                HOperatorSet.GenEmptyObj(out ho_Image);
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, openFileDialog.FileName);    //加载图像
                HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);  //获取图像的宽和高
                HOperatorSet.CountChannels(ho_Image, out hv_Channel);
                this.lab_ImageInfo.Text = hv_ImageWidth.L.ToString() + "*" + hv_ImageHeight.L.ToString() + "*" + hv_Channel.L.ToString();
                int p = openFileDialog.FileName.LastIndexOf("\\");
                string imageName = openFileDialog.FileName.Substring(p + 1);
                this.lab_imageName.Text = imageName;     //图像名称
                ho_AdaptiveImage(hv_ImageWidth, hv_ImageHeight);
                ShowDetectContour();
            }
        }
        private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                mouseDownPoint.X = (int)e.X;
                mouseDownPoint.Y = (int)e.Y;
                mouseDownTime.Reset();  //右键获取点击的时间
                mouseDownTime.Start();
            }
        }
        private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
        {
            double scaleWidth = scaleE_X - scaleS_X;
            double scaleHeight = scaleE_Y - scaleS_Y;
            double coorX = (scaleWidth * e.X / hWindowControl1.ImagePart.Width) + scaleS_X;
            double coorY = (scaleHeight * e.Y / hWindowControl1.ImagePart.Height) + scaleS_Y;
            this.lab_Coor.Text = "Coor: " + coorX.ToString("f0") + "*" + coorY.ToString("f0");
            if (ho_Image != null && ho_Image.IsInitialized() && coorX > 0 && coorY > 0 && coorX < hv_ImageWidth && coorY < hv_ImageHeight)
            {
                HTuple hv_Grayval = new HTuple();
                HOperatorSet.GetGrayval(ho_Image, coorY, coorX, out hv_Grayval);
                if (hv_Channel == 3)
                {
                    if (hv_Grayval.Length > 0)
                    {
                        this.lab_GrayValue.Text = "Rgb: " + hv_Grayval[0].D.ToString("f0") + "*" + hv_Grayval[1].D.ToString("f0") + "*" + hv_Grayval[2].D.ToString("f0");
                    }
                }
                if (hv_Channel == 1)
                {
                    if (hv_Grayval.Length > 0)
                    {
                        this.lab_GrayValue.Text = "Rgb: " + hv_Grayval[0].D.ToString("f0");
                    }
                }
            }
            if (e.Button == MouseButtons.Right && ho_Image != null)
            {
                keyNum++;
                if (keyNum > 100000) keyNum = 0; //防止一直增加
                if (keyNum % 8 == 0)     //降低触发的频率
                {
                    double ratio_X = scaleWidth / hWindowControl1.ImagePart.Width;
                    double ratio_Y = scaleHeight / hWindowControl1.ImagePart.Height;
                    int moveX, moveY;   //X方向，Y方向移动大小。
                    hWindowControl1.Cursor = Cursors.SizeAll;
                    moveX = (int)e.X - mouseDownPoint.X;
                    moveY = (int)e.Y - mouseDownPoint.Y;
                    scaleS_Y = scaleS_Y - moveY * ratio_Y;
                    scaleS_X = scaleS_X - moveX * ratio_X;
                    scaleE_Y = scaleE_Y - moveY * ratio_Y;
                    scaleE_X = scaleE_X - moveX * ratio_X;
                    if (scaleE_X - scaleS_X < 10 || scaleE_Y - scaleS_Y < 10) return;
                    HOperatorSet.SetPart(hWindowControl1.HalconWindow, scaleS_Y, scaleS_X, scaleE_Y, scaleE_X);
                    ShowDetectContour();
                    mouseDownPoint.X = (int)e.X;
                    mouseDownPoint.Y = (int)e.Y;
                }
            }
        }
        private void hWindowControl1_HMouseUp(object sender, HMouseEventArgs e)
        {

        }
        private void hWindowControl1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                mouseDownTime.Stop();
                if (mouseDownTime.ElapsedMilliseconds < 200 && !isPaintShape)
                {
                    this.contextMenuStrip1.Show(this.hWindowControl1, new Point((int)e.X, (int)e.Y));
                }
            }
        }
        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            if (ho_Image != null && hv_ImageWidth != null)
            {
                wheelNum++;
                if (wheelNum > 10000) wheelNum = 0;
                if (wheelNum % 4 == 0)
                {
                    hWindowControl1.Focus();
                    double ratio = (scaleE_Y - scaleS_Y) / (scaleE_X - scaleS_X);
                    double zoomRatio = hv_ImageWidth.D / 2000;
                    double tempS_X = scaleS_X + e.Delta * zoomRatio;
                    double tempS_Y = scaleS_Y + e.Delta * zoomRatio * ratio;
                    double tempE_X = scaleE_X - e.Delta * zoomRatio;
                    double tempE_Y = scaleE_Y - e.Delta * zoomRatio * ratio;
                    if (tempE_X - tempS_X < 10 || tempE_Y - tempS_Y < 10)
                        return;
                    else
                    {
                        scaleS_X = tempS_X;
                        scaleS_Y = tempS_Y;
                        scaleE_X = tempE_X;
                        scaleE_Y = tempE_Y;
                    }
                    HOperatorSet.SetPart(hWindowControl1.HalconWindow, scaleS_Y, scaleS_X, scaleE_Y, scaleE_X);
                    ShowDetectContour();
                }
            }
        }
        private void btn_setAdvancRegion_Click(object sender, EventArgs e)
        {
            if (isPaintShape)
            {
                MessageBox.Show("请先右键确认绘图效果！");
                return;
            }
            if (unionROI != null && ho_Image != null)
            {
                HOperatorSet.GenEmptyObj(out ho_ImageReduced);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, unionROI, out ho_ImageReduced);
                HOperatorSet.SetColor(hWindowControl1.HalconWindow, "coral");
                HOperatorSet.DispObj(unionROI, hWindowControl1.HalconWindow);
            }
            else
                MessageBox.Show("请先设置ROI区域");
        }
        private void tB_minGrayBinary_Scroll(object sender, EventArgs e)
        {
            minGray = this.tB_minGrayBinary.Value;
            this.binaryMinGray.Value = minGray;
            if (minGray <= maxGray)
            {
                if (ho_Image != null)
                {
                    HObject binaryImage = null;
                    HOperatorSet.GenEmptyObj(out binaryImage);
                    if (ho_ImageReduced != null)
                    {
                        if (unionROI != null && unionROI.IsInitialized())
                        {
                            binaryImage.Dispose();
                            HOperatorSet.Threshold(ho_ImageReduced, out binaryImage, minGray, maxGray);
                            if (this.tB_Dilation.Value > 0)
                            {
                                HObject ho_dilateImage = null;
                                HOperatorSet.GenEmptyObj(out ho_dilateImage);
                                ho_dilateImage.Dispose();
                                HOperatorSet.DilationRectangle1(binaryImage, out ho_dilateImage, this.tB_Dilation.Value, this.tB_Dilation.Value);     //膨胀   
                                binaryImage.Dispose();
                                binaryImage = ho_dilateImage.Clone();
                            }
                            if (this.tB_Erosion.Value > 0)
                            {
                                HObject ho_erosionImage = null;
                                HOperatorSet.GenEmptyObj(out ho_erosionImage);
                                ho_erosionImage.Dispose();
                                HOperatorSet.ErosionRectangle1(binaryImage, out ho_erosionImage, this.tB_Erosion.Value, this.tB_Erosion.Value);         //腐蚀
                                binaryImage.Dispose();
                                binaryImage = ho_erosionImage.Clone();
                            }
                            unionROI.Dispose();
                            unionROI = binaryImage.Clone();
                            binaryImage.Dispose();
                            hWindowControl1.HalconWindow.ClearWindow();
                            HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
                            HOperatorSet.DispObj(unionROI, hWindowControl1.HalconWindow);
                            Thread.Sleep(100);
                        }
                    }
                    else
                    {
                        binaryImage.Dispose();
                        HOperatorSet.Threshold(ho_Image, out binaryImage, minGray, maxGray);
                        hWindowControl1.HalconWindow.ClearWindow();
                        HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                        HOperatorSet.DispObj(binaryImage, hWindowControl1.HalconWindow);
                        binaryImage.Dispose();
                    }
                }
            }
            else
            {
                MessageBox.Show("高阈值必须大于低阈值");
            }
            Thread.Sleep(10);
        }

        private void tB_maxGrayBinary_Scroll(object sender, EventArgs e)
        {
            maxGray = this.tB_maxGrayBinary.Value;
            this.binaryMaxGray.Value = maxGray;
            if (minGray <= maxGray)
            {
                if (ho_Image != null)
                {
                    HObject binaryImage = null;
                    HOperatorSet.GenEmptyObj(out binaryImage);
                    if (ho_ImageReduced != null)
                    {
                        if (unionROI != null && unionROI.IsInitialized())
                        {
                            binaryImage.Dispose();
                            HOperatorSet.Threshold(ho_ImageReduced, out binaryImage, minGray, maxGray);
                            if (this.tB_Dilation.Value > 0)
                            {
                                HObject ho_dilateImage = null;
                                HOperatorSet.GenEmptyObj(out ho_dilateImage);
                                ho_dilateImage.Dispose();
                                HOperatorSet.DilationRectangle1(binaryImage, out ho_dilateImage, this.tB_Dilation.Value, this.tB_Dilation.Value);     //膨胀   
                                binaryImage.Dispose();
                                binaryImage = ho_dilateImage.Clone();
                            }
                            if (this.tB_Erosion.Value > 0)
                            {
                                HObject ho_erosionImage = null;
                                HOperatorSet.GenEmptyObj(out ho_erosionImage);
                                ho_erosionImage.Dispose();
                                HOperatorSet.ErosionRectangle1(binaryImage, out ho_erosionImage, this.tB_Erosion.Value, this.tB_Erosion.Value);         //腐蚀
                                binaryImage.Dispose();
                                binaryImage = ho_erosionImage.Clone();
                            }
                            unionROI.Dispose();
                            unionROI = binaryImage.Clone();
                            binaryImage.Dispose();
                            hWindowControl1.HalconWindow.ClearWindow();
                            HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                            HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
                            HOperatorSet.DispObj(unionROI, hWindowControl1.HalconWindow);
                            Thread.Sleep(100);
                        }
                    }
                    else
                    {
                        binaryImage.Dispose();
                        HOperatorSet.Threshold(ho_Image, out binaryImage, minGray, maxGray);
                        hWindowControl1.HalconWindow.ClearWindow();
                        HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                        HOperatorSet.DispObj(binaryImage, hWindowControl1.HalconWindow);
                        binaryImage.Dispose();
                    }
                }
            }
            else
            {

                MessageBox.Show("高阈值必须大于低阈值");
            }
            Thread.Sleep(10);
        }

        private void tB_Dilation_Scroll(object sender, EventArgs e)
        {
            if (ho_Image != null && this.tB_Dilation.Value > 0)
            {
                if (unionROI != null && unionROI.IsInitialized() && ho_ImageReduced != null)
                {
                    HObject binaryImage = null;
                    HOperatorSet.GenEmptyObj(out binaryImage);
                    binaryImage.Dispose();
                    HOperatorSet.Threshold(ho_ImageReduced, out binaryImage, this.tB_minGrayBinary.Value, this.tB_maxGrayBinary.Value);
                    HObject ho_dilateImage = null;
                    HOperatorSet.GenEmptyObj(out ho_dilateImage);
                    ho_dilateImage.Dispose();
                    HOperatorSet.DilationRectangle1(binaryImage, out ho_dilateImage, this.tB_Dilation.Value, this.tB_Dilation.Value);     //膨胀  
                    unionROI.Dispose();
                    if (this.tB_Erosion.Value > 0)
                    {
                        HOperatorSet.ErosionRectangle1(ho_dilateImage, out unionROI, this.tB_Erosion.Value, this.tB_Erosion.Value);         //腐蚀
                    }
                    else
                    {
                        unionROI = ho_dilateImage.Clone();
                    }
                    ho_dilateImage.Dispose();
                    binaryImage.Dispose();
                    this.regionDilation.Value = this.tB_Dilation.Value;
                    this.regionErosion.Value = this.tB_Erosion.Value;
                    hWindowControl1.HalconWindow.ClearWindow();
                    HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                    HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
                    HOperatorSet.DispObj(unionROI, hWindowControl1.HalconWindow);
                    Thread.Sleep(100);
                }
            }
        }

        private void tB_Erosion_Scroll(object sender, EventArgs e)
        {
            if (ho_Image != null && this.tB_Erosion.Value > 0)
            {
                if (unionROI != null && unionROI.IsInitialized())
                {
                    HObject binaryImage = null;
                    HOperatorSet.GenEmptyObj(out binaryImage);
                    binaryImage.Dispose();
                    HOperatorSet.Threshold(ho_ImageReduced, out binaryImage, this.tB_minGrayBinary.Value, this.tB_maxGrayBinary.Value);
                    if (this.tB_Dilation.Value > 0)
                    {
                        HObject ho_dilateImage = null;
                        HOperatorSet.GenEmptyObj(out ho_dilateImage);
                        ho_dilateImage.Dispose();
                        HOperatorSet.DilationRectangle1(binaryImage, out ho_dilateImage, this.tB_Dilation.Value, this.tB_Dilation.Value);     //膨胀   
                        unionROI.Dispose();
                        HOperatorSet.ErosionRectangle1(ho_dilateImage, out unionROI, this.tB_Erosion.Value, this.tB_Erosion.Value);         //腐蚀
                        ho_dilateImage.Dispose();
                    }
                    else
                    {
                        unionROI.Dispose();
                        HOperatorSet.ErosionRectangle1(binaryImage, out unionROI, this.tB_Erosion.Value, this.tB_Erosion.Value);         //腐蚀
                    }
                    binaryImage.Dispose();
                    this.regionDilation.Value = this.tB_Dilation.Value;
                    this.regionErosion.Value = this.tB_Erosion.Value;
                    hWindowControl1.HalconWindow.ClearWindow();
                    HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                    HOperatorSet.SetColor(hWindowControl1.HalconWindow, "blue");
                    HOperatorSet.DispObj(unionROI, hWindowControl1.HalconWindow);
                    Thread.Sleep(100);
                }
            }
        }

        private void binaryMinGray_ValueChanged(object sender, EventArgs e)
        {
            minGray = (int)this.binaryMinGray.Value;
            this.tB_minGrayBinary.Value = minGray;
            if (minGray <= maxGray)
            {
                if (ho_Image != null)
                {
                    HObject binaryImage = null;
                    HOperatorSet.GenEmptyObj(out binaryImage);
                    binaryImage.Dispose();
                    HOperatorSet.Threshold(ho_Image, out binaryImage, minGray, maxGray);
                    Thread.Sleep(100);
                    HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                    HOperatorSet.DispObj(binaryImage, hWindowControl1.HalconWindow);
                    binaryImage.Dispose();
                }
            }
            else
            {
                MessageBox.Show("高阈值必须大于低阈值");
            }
        }

        private void binaryMaxGray_ValueChanged(object sender, EventArgs e)
        {
            maxGray = (int)this.binaryMaxGray.Value;
            this.tB_maxGrayBinary.Value = maxGray;
            if (minGray <= maxGray)
            {
                if (ho_Image != null)
                {
                    HObject binaryImage = null;
                    HOperatorSet.GenEmptyObj(out binaryImage);
                    binaryImage.Dispose();
                    HOperatorSet.Threshold(ho_Image, out binaryImage, minGray, maxGray);
                    Thread.Sleep(100);
                    HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
                    HOperatorSet.DispObj(binaryImage, hWindowControl1.HalconWindow);
                    binaryImage.Dispose();
                }
            }
            else
            {

                MessageBox.Show("高阈值必须大于低阈值");
            }
        }

        private void regionDilation_ValueChanged(object sender, EventArgs e)
        {
            this.tB_Dilation.Value = (int)this.regionDilation.Value;
            tB_Dilation_Scroll(null, null);
        }

        private void regionErosion_ValueChanged(object sender, EventArgs e)
        {
            this.tB_Erosion.Value = (int)this.regionErosion.Value;
            tB_Erosion_Scroll(null, null);
        }

        #endregion

        private void btn_loadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图片文件(*.jpg,*.tif,*.bmp,*.Png)|*.jpg;*.tif;*.bmp;*.Png";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                HOperatorSet.GenEmptyObj(out ho_Image);
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, openFileDialog.FileName);    //加载图像
                HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);  //获取图像的宽和高
                HOperatorSet.CountChannels(ho_Image, out hv_Channel);
                this.lab_ImageInfo.Text = hv_ImageWidth.L.ToString() + "*" + hv_ImageHeight.L.ToString() + "*" + hv_Channel.L.ToString();
                int p = openFileDialog.FileName.LastIndexOf("\\");
                string imageName = openFileDialog.FileName.Substring(p + 1);
                this.lab_imageName.Text = imageName;     //图像名称
                ho_AdaptiveImage(hv_ImageWidth, hv_ImageHeight);
                ShowDetectContour();
            }
        }

        private void btn_excuteCurItem_Click(object sender, EventArgs e)
        {
            if (ho_Image == null)
            {
                MessageBox.Show("请先加载图像！");
                return;
            }
            if (unionROI == null)
            {
                MessageBox.Show("请先绘制ROI！");
                return;
            }
            try
            {
                HTuple hv_cfgPara = new HTuple();
                hv_cfgPara[0] = this.cB_detectType.SelectedIndex;
                hv_cfgPara[1] = (int)this.NUD_MinGray.Value;
                hv_cfgPara[2] = (int)this.NUD_MaxGray.Value;
                hv_cfgPara[3] = (int)this.VarMask.Value;                  //var阈值mask
                hv_cfgPara[4] = double.Parse(this.tB_VarDeviation.Text);  //var阈值偏差因子
                hv_cfgPara[5] = (int)(this.VarThreshold.Value);
                switch (this.cB_BlackOrWhite.SelectedIndex)  //缺陷类型
                {
                    case 0:
                        hv_cfgPara[6] = "dark";
                        break;
                    case 1:
                        hv_cfgPara[6] = "light";
                        break;
                    case 2:
                        hv_cfgPara[6] = "light";
                        break;
                    case 3:
                        hv_cfgPara[6] = "not_equal";
                        break;
                }
                hv_cfgPara[7] = double.Parse(this.ClosingRadius.Text);   //闭运算
                hv_cfgPara[8] = double.Parse(this.OpeningRadius.Text);   //开运算
                hv_cfgPara[9] = double.Parse(this.tB_MeanRadius.Text);   //平滑系数
                hv_cfgPara[10] = double.Parse(this.defectMinArea.Text);  //最小面积
                hv_cfgPara[11] = double.Parse(this.defectMaxArea.Text);  //最大面积
                if (this.cB_isAddArea.Checked)
                    hv_cfgPara[12] = 0;
                else
                    hv_cfgPara[12] = 1;
                hv_cfgPara[13] = (int)this.defectMinMean.Value;   //最小亮度
                hv_cfgPara[14] = (int)this.defectMaxMean.Value;   //最大亮度
                if (this.cB_isAddMean.Checked)
                    hv_cfgPara[15] = 0;
                else
                    hv_cfgPara[15] = 1;
                hv_cfgPara[16] = (int)this.defectMinNum.Value;   //最小数量
                hv_cfgPara[17] = (int)this.defectMaxNum.Value;   //最大数量
                if (this.cB_isAddNum.Checked)
                    hv_cfgPara[18] = 0;
                else
                    hv_cfgPara[18] = 1;
                hv_cfgPara[19] = (int)this.areaFilter.Value;   //面积过滤
                HTuple hv_defectArea = null, hv_defectRow = null, hv_defectCol = null, hv_defectMean = null,
                        hv_detectResult = null;
                if (ho_defectContour == null) HOperatorSet.GenEmptyObj(out ho_defectContour);
                ho_defectContour.Dispose();
                detectTime.Reset();
                detectTime.Start();
                if (this.cB_inheritModel.Checked)
                {
                    //继承于模板
                    HTuple hv_matchModel = null;
                    HTuple hv_modelRow = new HTuple();
                    HTuple hv_modelCol = new HTuple();
                    HTuple hv_modelAngle = new HTuple();
                    HTuple hv_modelScore = new HTuple();
                    if (curMatchPara.modelMatchType == 0)   //基于形状
                    {
                        string shapeModelFile = modelImageFold + "\\model.shm";
                        if (!File.Exists(shapeModelFile))
                        {
                            MessageBox.Show("模板文件不存在，请先加载模型！");
                            return;
                        }
                        HOperatorSet.ReadShapeModel(shapeModelFile, out hv_matchModel);  //加载模板
                        HOperatorSet.FindShapeModel(ho_Image, hv_matchModel, (new HTuple(curMatchPara.seekAngleStart)).TupleRad(), (new HTuple(curMatchPara.seekAngleRange)).TupleRad(), curMatchPara.seekModelScore * 0.01,
                           curMatchPara.seekModelNum, 0.5, "least_squares", 0, 0.9, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore); //查找模板 
                        HOperatorSet.ClearShapeModel(hv_matchModel);                     //清除模板
                    }
                    else if (curMatchPara.modelMatchType == 1)  //基于灰度
                    {
                        string nccModelFile = modelImageFold + "\\model.ncm";
                        if (!File.Exists(nccModelFile))
                        {
                            MessageBox.Show("模板文件不存在，请先加载模型！");
                            return;
                        }
                        HOperatorSet.ReadNccModel(nccModelFile, out hv_matchModel);  //加载模板
                        HOperatorSet.FindNccModel(ho_Image, hv_matchModel, (new HTuple(curMatchPara.seekAngleStart)).TupleRad(), (new HTuple(curMatchPara.seekAngleRange)).TupleRad(), curMatchPara.seekModelScore * 0.01, 
                            curMatchPara.seekModelNum, 0.5, "true", 0, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore);
                        HOperatorSet.ClearNccModel(hv_matchModel);                   //清除模板

                    }
                    if (hv_modelScore.Length != 1)
                    {
                        MessageBox.Show("模板定位失败！");
                        return;
                    }
                    HTuple hv_HomMat2D;
                    HOperatorSet.VectorAngleToRigid(curMatchPara.modelLocationRow, curMatchPara.modelLocationCol, 0, hv_modelRow[0],
                           hv_modelCol[0], hv_modelAngle[0], out hv_HomMat2D);   //根据模板坐标，求出放射矩阵
                    if (ho_AffinTransRegion == null) HOperatorSet.GenEmptyObj(out ho_AffinTransRegion);
                    ho_AffinTransRegion.Dispose();
                    HOperatorSet.AffineTransRegion(unionROI, out ho_AffinTransRegion, hv_HomMat2D, "nearest_neighbor");
                    halconAlgorithm.DefectDetect(ho_Image, ho_AffinTransRegion, out ho_defectContour, hv_cfgPara, out hv_defectArea, out hv_defectRow, out hv_defectCol, out hv_defectMean, out hv_detectResult);
                }
                else
                {
                    halconAlgorithm.DefectDetect(ho_Image, unionROI, out ho_defectContour, hv_cfgPara, out hv_defectArea, out hv_defectRow, out hv_defectCol, out hv_defectMean, out hv_detectResult);
                }                
                detectTime.Stop();
                ShowDetectContour();
                string detectResultShow = "检测结果：";
                
                this.dataGridView1.Rows.Clear();
                if (hv_detectResult == 1)   //0为OK，1为NG
                {
                    for (int j = 0; j < hv_defectArea.Length; j++)
                    {
                        if (hv_defectArea.LArr[j] > 0)
                        {
                            this.dataGridView1.Rows.Add();
                            this.dataGridView1.Rows[j].Cells[0].Value = (j + 1).ToString();
                            this.dataGridView1.Rows[j].Cells[1].Value = hv_defectArea.LArr[j].ToString();
                            this.dataGridView1.Rows[j].Cells[2].Value = hv_defectMean.DArr[j].ToString("f1");
                            this.dataGridView1.Rows[j].Cells[3].Value = hv_defectRow.DArr[j].ToString("f0") + "*" + hv_defectCol.DArr[j].ToString("f0");
                        }
                    }
                    detectResultShow = detectResultShow + "NG，";
                }
                else
                    detectResultShow = detectResultShow + "OK，";
                this.lab_executeState.Text = detectResultShow + "用时：" + detectTime.ElapsedMilliseconds.ToString() + "ms";
            }
            catch(Exception ex)
            {
                MessageBox.Show("检测失败！" + ex.Message);
            }
        }

        private void btn_quit_Click(object sender, EventArgs e)
        {
            if (ho_Image != null) ho_Image.Dispose();
            if (ho_defectContour != null) ho_defectContour.Dispose();
            if (unionROI != null) unionROI.Dispose();
            GC.Collect();
            this.Close();
        }

        private void btn_savePara_Click(object sender, EventArgs e)
        {
            try
            {
                if (unionROI == null)
                {
                    MessageBox.Show("请先绘制ROI！");
                    return;
                }
                outBlobDetectPara.detectROI = unionROI.CopyObj(1, -1);
                outBlobDetectPara.detectType = this.cB_detectType.SelectedIndex;  //检测方式
                outBlobDetectPara.minGray = (int)NUD_MinGray.Value;  //普通阈值最小灰度
                outBlobDetectPara.maxGray = (int)NUD_MaxGray.Value;  //普通阈值最小灰度
                outBlobDetectPara.mask = (int)VarMask.Value;                                 //va阈值
                outBlobDetectPara.stdDevScale = double.Parse(tB_VarDeviation.Text);          //vask偏差因子
                outBlobDetectPara.absThreshold = (int)VarThreshold.Value;                    //vask阈值

                double meanRadius = -1;
                if (double.TryParse(this.tB_MeanRadius.Text, out meanRadius))
                {
                    if (meanRadius >= 1)
                    {
                        outBlobDetectPara.meanRadius = meanRadius;               //平滑系数
                    }
                    else
                    {
                        MessageBox.Show("保存参数失败！，平滑系数不能小于1");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("保存参数失败！，平滑系数格式输入有误！");
                    return;
                }
                outBlobDetectPara.lightDark = this.cB_BlackOrWhite.SelectedIndex;            //检测类型 
                outBlobDetectPara.closeRadius = double.Parse(this.ClosingRadius.Text);       //闭运算
                outBlobDetectPara.openRadius = double.Parse(this.OpeningRadius.Text);        //开运算
                outBlobDetectPara.areaFilter = (int)this.areaFilter.Value;                   //面积过滤
                outBlobDetectPara.minArea = double.Parse(this.defectMinArea.Text);   //最小面积，确保是像素单位
                outBlobDetectPara.maxArea = double.Parse(this.defectMaxArea.Text); //最大面积
                if (this.cB_isAddArea.Checked)
                {
                    outBlobDetectPara.addArea = true;
                }
                else
                {
                    outBlobDetectPara.addArea = false;
                }
                outBlobDetectPara.minMean = (int)this.defectMinMean.Value;
                outBlobDetectPara.maxMean = (int)this.defectMaxMean.Value;
                if (this.cB_isAddMean.Checked)
                {
                    outBlobDetectPara.addMean = true;
                }
                else
                {
                    outBlobDetectPara.addMean = false;
                }
                outBlobDetectPara.minNum = (int)this.defectMinNum.Value;
                outBlobDetectPara.maxNum = (int)this.defectMaxNum.Value;
                if (this.cB_isAddNum.Checked)
                {
                    outBlobDetectPara.addNum = true;
                }
                else
                {
                    outBlobDetectPara.addNum = false;
                }
                if (this.cB_inheritModel.Checked)
                    outBlobDetectPara.isInheritModel = true;
                else
                    outBlobDetectPara.isInheritModel = false;
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存图像失败！" + ex.Message);
            }

        }

        private void btn_loadModelImage_Click(object sender, EventArgs e)
        {
            //加载模板图
            string modelImagePath = modelImageFold + "\\image.bmp";
            if (File.Exists(modelImagePath))
            {
                if (ho_Image == null) HOperatorSet.GenEmptyObj(out ho_Image);
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, modelImagePath);    //加载图像
                HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);  //获取图像的宽和高
                HOperatorSet.CountChannels(ho_Image, out hv_Channel);
                this.lab_ImageInfo.Text = hv_ImageWidth.L.ToString() + "*" + hv_ImageHeight.L.ToString() + "*" + hv_Channel.L.ToString();
                this.lab_imageName.Text = "image.bmp";     //图像名称
                ho_AdaptiveImage(hv_ImageWidth, hv_ImageHeight);
                ShowDetectContour();
                //hWindowControl1.HalconWindow.ClearWindow();
                //HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);

            }
            else
            {
                MessageBox.Show("加载失败，模板图像不存在！");
            }
        }

        private void cB_detectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cB_detectType.SelectedIndex == 0)  //普通阈值
            {
                this.gB_thresCommon.Visible = true;
                this.gB_thresCommon.Dock = DockStyle.Fill;
                this.gB_thresHigh.Visible = false;
            }
            else if (this.cB_detectType.SelectedIndex == 1)  //高级阈值
            {
                this.gB_thresHigh.Visible = true;
                this.gB_thresHigh.Dock = DockStyle.Fill;
                this.gB_thresCommon.Visible = false;
            }
        }

    }
}
